{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from pathlib import Path"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# dset_dir = \"E:\\\\datasets\\\\spacenet\\\\train\"\n",
    "dset_dir = \"E:/datasets/spacenet-dataset/spacenet/SN6_buildings/train/AOI_11_Rotterdam\"\n",
    "# fold_list = [\"PAN\", \"MS\", \"SAR-Intensity\"]\n",
    "fold_list = [\"SAR-Intensity\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "dset_dict = {}\n",
    "for data_type in fold_list:\n",
    "    dset_path = Path(os.path.join(dset_dir, data_type))\n",
    "    cnt = 0\n",
    "    tmp_list = []\n",
    "    for path in dset_path.iterdir():\n",
    "        if path.is_file():\n",
    "            # tmp_list.append(os.path.splitext(path.parts[-1])[0])\n",
    "            tmp_list.append(str(path))\n",
    "    dset_dict[data_type] = tmp_list\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3401\n"
     ]
    }
   ],
   "source": [
    "for data_type in fold_list:\n",
    "    print(dset_dict[data_type].__len__())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'E:\\\\datasets\\\\spacenet-dataset\\\\spacenet\\\\SN6_buildings\\\\train\\\\AOI_11_Rotterdam\\\\PS-RGB\\\\SN6_Train_AOI_11_Rotterdam_PS-RGB_20190804111224_20190804111453_tile_8681.tif'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dset_dict['PS-RGB'][1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 查找每个图像的值范围"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "from osgeo import gdal\n",
    "import numpy as np\n",
    "from tqdm import tqdm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "traverse PS-RGB: 100%|██████████| 3401/3401 [02:26<00:00, 23.19it/s]\n"
     ]
    }
   ],
   "source": [
    "# 仅有单通道的图像的关键词\n",
    "# exception = ['PAN']\n",
    "exception = []\n",
    "inf_ = 65536\n",
    "value_dict = {}\n",
    "for data_type in dset_dict:\n",
    "    img_np = gdal.Open(dset_dict[data_type][0]).ReadAsArray()\n",
    "    max = np.zeros(img_np.shape[0]) if data_type not in exception else np.zeros(1)\n",
    "    min = np.ones_like(max) * inf_\n",
    "    del img_np\n",
    "    if data_type in exception:\n",
    "        axis = (0, 1)\n",
    "    else:\n",
    "        axis = (1, 2)\n",
    "    iter_ = tqdm(dset_dict[data_type])\n",
    "    for name in iter_:\n",
    "        img_np = gdal.Open(name).ReadAsArray()\n",
    "        max_tmp = img_np.max(axis=axis)\n",
    "        min_tmp = img_np.min(axis=axis)\n",
    "        max = np.vstack((max, max_tmp)).max(axis=0)\n",
    "        min = np.vstack((min, min_tmp)).min(axis=0)\n",
    "        del img_np\n",
    "        iter_.set_description(f\"traverse {data_type}\")\n",
    "    value_dict[data_type+'max'] = max\n",
    "    value_dict[data_type+'min'] = min\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'PS-RGBmax': array([255., 255., 255.]), 'PS-RGBmin': array([0., 0., 0.])}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "value_dict"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "8ab0d968002a578cf1e1aa041721a175249ba338f6da29efa5ef1a380c630376"
  },
  "kernelspec": {
   "display_name": "Python 3.8.5 64-bit ('base': conda)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
